package com.edu.buaa;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;

public class RegisterActivity extends AppCompatActivity {
    private static final String TAG = "RegisterActivity";

    private EditText etUsername, etPassword, etConfirmPassword, etEmail;
    private Button btnRegister;
    private Button btnBack;
    private DatabaseHelper databaseHelper;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);

        Log.d(TAG, "RegisterActivity onCreate");

        try {
            initViews();
            databaseHelper = new DatabaseHelper(this);
            setupClickListeners();
            Log.d(TAG, "RegisterActivity initialized successfully");
        } catch (Exception e) {
            Log.e(TAG, "Error initializing RegisterActivity: " + e.getMessage(), e);
            Toast.makeText(this, "页面初始化失败", Toast.LENGTH_LONG).show();
        }
    }

    private void initViews() {
        Log.d(TAG, "Initializing RegisterActivity views");
        etUsername = findViewById(R.id.etUsername);
        etPassword = findViewById(R.id.etPassword);
        etConfirmPassword = findViewById(R.id.etConfirmPassword);
        etEmail = findViewById(R.id.etEmail);
        btnRegister = findViewById(R.id.btnRegister);
        btnBack = findViewById(R.id.btnBack);
    }

    private void setupClickListeners() {
        btnRegister.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.d(TAG, "Register button clicked");
                registerUser();
            }
        });

        // 返回按钮点击事件
        btnBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.d(TAG, "Back button clicked");
                goBackToLogin();
            }
        });
    }

    private void goBackToLogin() {
        Intent intent = new Intent(RegisterActivity.this, LoginActivity.class);
        startActivity(intent);
        finish();
    }

    @Override
    public void onBackPressed() {
        // 物理返回键也返回登录页面
        goBackToLogin();
    }

    private void registerUser() {
        try {
            String username = etUsername.getText().toString().trim();
            String password = etPassword.getText().toString().trim();
            String confirmPassword = etConfirmPassword.getText().toString().trim();
            String email = etEmail.getText().toString().trim();

            Log.d(TAG, "Attempting registration for user: " + username);

            if (username.isEmpty() || password.isEmpty() || confirmPassword.isEmpty()) {
                Toast.makeText(this, "请填写所有必填字段", Toast.LENGTH_SHORT).show();
                return;
            }

            if (!password.equals(confirmPassword)) {
                Toast.makeText(this, "两次输入的密码不一致", Toast.LENGTH_SHORT).show();
                return;
            }

            if (password.length() < 6) {
                Toast.makeText(this, "密码长度至少6位", Toast.LENGTH_SHORT).show();
                return;
            }

            if (databaseHelper.checkUsername(username)) {
                Toast.makeText(this, "用户名已存在", Toast.LENGTH_SHORT).show();
                return;
            }

            if (databaseHelper.addUser(username, password, email)) {
                Toast.makeText(this, "注册成功，请登录", Toast.LENGTH_SHORT).show();
                Log.d(TAG, "Registration successful for user: " + username);

                // 返回登录页面
                goBackToLogin();
            } else {
                Toast.makeText(this, "注册失败，请重试", Toast.LENGTH_SHORT).show();
                Log.e(TAG, "Registration failed for user: " + username);
            }
        } catch (Exception e) {
            Log.e(TAG, "Error during registration: " + e.getMessage(), e);
            Toast.makeText(this, "注册过程出错: " + e.getMessage(), Toast.LENGTH_LONG).show();
        }
    }
}